package com.sctvcloud.qingchuan.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;

import com.jiongbull.jlog.JLog;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.application.Cache;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.beans.AccountBean;
import com.sctvcloud.qingchuan.beans.AccountCommentBean;
import com.sctvcloud.qingchuan.beans.AttentionBean;
import com.sctvcloud.qingchuan.http.AbsNetCallBack;
import com.sctvcloud.qingchuan.http.NetUtils;
import com.sctvcloud.qingchuan.ui.dialog.CommentAccountDiaFragment;
import com.sctvcloud.qingchuan.ui.dialog.CommentDiaFragment;
import com.sctvcloud.qingchuan.ui.utils.DataUtils;
import com.sctvcloud.qingchuan.ui.utils.SkipUtil;
import com.sctvcloud.qingchuan.utils.UserManager;

import java.util.HashMap;
import java.util.Map;

import okhttp3.RequestBody;

/**
 * 华栖云融媒相关 基础Activity
 */
public class BaseAccountActivity extends BaseCanPlayActivity {

    protected CommentAccountDiaFragment commentDiaFragment;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public void toAttentionAccount(String accountId, final OnAttentionListener listener) {
        if (null != UserManager.getInstance().getUser() && null != UserManager.getInstance().getUser().getAccountUser()) {
            String accessToken = UserManager.getInstance().getUser().getAccountUser().getAccess_token();
            Map<String, String> formMaildata = new HashMap<>();
            formMaildata.put("tenantId", Constances.TENANT_ID);
            formMaildata.put("userId", accountId);

            Map<String, RequestBody> bodyMap = DataUtils.generateRequestBody(formMaildata);
            NetUtils.getNetAdapter().toAttentionAccountString(getOwnerName(), accessToken, bodyMap, new AbsNetCallBack<String>(String.class) {
                @Override
                public void onSucStr(String news) {
                    if (!TextUtils.isEmpty(news)) {
                        try {
                            AttentionBean mAttention = gson.fromJson(news, AttentionBean.class);
                            if (mAttention != null) {
                                if (null != listener) {
                                    toast("关注成功");
                                    listener.OnAttention(true, mAttention.getAttentionedUserNums());
                                }
                            } else {
                                if (null != listener) {
                                    toast("关注失败");
                                    listener.OnAttention(false, 0);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            if (null != listener) {
                                toast("关注失败");
                                listener.OnAttention(false, 0);
                            }
                        }
                    } else {
                        onError(null, "数据异常");
                        if (null != listener) {
                            toast("关注失败");
                            listener.OnAttention(false, 0);
                        }
                    }
                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
//                    toast(errorMsg);
                    if (null != listener) {
                        toast("关注失败");
                        listener.OnAttention(false, 0);
                    }
                }

                @Override
                public void onEnd() {
                }
            });
        }else{
            toLogin();
        }


    }

    public void toUnAttentionAccount(String accountId, final OnAttentionListener listener) {
        if (null != UserManager.getInstance().getUser() && null != UserManager.getInstance().getUser().getAccountUser()) {
            String accessToken = UserManager.getInstance().getUser().getAccountUser().getAccess_token();
            Map<String, String> formMaildata = new HashMap<>();
            formMaildata.put("tenantId", Constances.TENANT_ID);
            formMaildata.put("userId", accountId);

            Map<String, RequestBody> bodyMap = DataUtils.generateRequestBody(formMaildata);
            NetUtils.getNetAdapter().toUnAttentionAccountString(getOwnerName(), accessToken, bodyMap, new AbsNetCallBack<String>(String.class) {
                @Override
                public void onSucStr(String news) {
                    if (!TextUtils.isEmpty(news)) {
                        try {
                            AttentionBean mAttention = gson.fromJson(news, AttentionBean.class);
                            if (mAttention != null) {
                                if (null != listener) {
                                    toast("取消关注成功");
                                    listener.OnUnAttention(true, mAttention.getAttentionedUserNums());
                                }
                            } else {
                                if (null != listener) {
                                    toast("取消关注失败");
                                    listener.OnUnAttention(false, 0);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            if (null != listener) {
                                toast("取消关注失败");
                                listener.OnUnAttention(false, 0);
                            }
                        }
                    } else {
                        onError(null, "数据异常");
                        if (null != listener) {
                            toast("取消关注失败");
                            listener.OnUnAttention(false, 0);
                        }
                    }
                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
//                    toast(errorMsg);
                    if (null != listener) {
                        toast("取消关注失败");
                        listener.OnUnAttention(false, 0);
                    }
                }

                @Override
                public void onEnd() {
                }
            });
        }else{
            toLogin();
        }


    }

    private AccountCommentBean commentContent;

    /**
     * 添加pingl
     *
     * @param comment
     * @param flag
     * @param listener
     */
    protected synchronized void showCommentDialog(AccountCommentBean comment, final int flag, final OnAddCommentListener listener) {
        this.commentContent = comment;
        FragmentManager fm = getSupportFragmentManager();
//        if (commentDiaFragment == null) {
        commentDiaFragment = new CommentAccountDiaFragment();
        commentDiaFragment.setSendComment(new CommentAccountDiaFragment.SendCommentListener() {
            @Override
            public void onCommentSend(String content) {
                commentContent.setContent(content);
                toAddComment(commentContent, flag, listener);
            }
        });
        commentDiaFragment.show(fm, COMMENT_TAG);
        fm.executePendingTransactions();
    }

    /**
     * 添加评论、回复评论
     *
     * @param commentContent 评论内容不符
     * @param flag           0：添加评论 1：添加评论回复
     * @param listener
     */
    public void toAddComment(AccountCommentBean commentContent, int flag, final OnAddCommentListener listener) {
        Map<String, String> formMaildata = new HashMap<>();
        formMaildata.put("relaId", commentContent.getRelaId());
        formMaildata.put("siteId", Constances.ACCOUNT_SITE_ID);
        formMaildata.put("content", commentContent.getContent());
//            formMaildata.put("nickName", commentContent.getNickName());
//            formMaildata.put("avatar", commentContent.getAvatar());
        formMaildata.put("type", commentContent.getType());//1：文稿评论 2：活动 3：评论回复
        formMaildata.put("uId", commentContent.getAddUser());
        formMaildata.put("flag", "" + flag);//评论标识 0：添加评论 1：添加评论回复
//            formMaildata.put("anonymousFlag", "" + commentContent.getAnonymousFlag());//0:不匿名显示,1:该评论匿名显示
        if (!TextUtils.isEmpty(commentContent.getId())) {
            formMaildata.put("beReplyUser", commentContent.getBeReplyUser());
            formMaildata.put("beReplyNickName", commentContent.getBeReplyNickName());
            formMaildata.put("beReplyAvatar", commentContent.getBeReplyAvatar());
            formMaildata.put("parentId", commentContent.getId());
        }


        Map<String, RequestBody> bodyMap = DataUtils.generateRequestBody(formMaildata);
        NetUtils.getNetAdapter().toAddCommentString(getOwnerName(), bodyMap, new AbsNetCallBack<String>(String.class) {
            @Override
            public void onSucStr(String news) {
                if (!TextUtils.isEmpty(news)) {
                    try {
                        AttentionBean mAttention = gson.fromJson(news, AttentionBean.class);
                        if (mAttention != null) {
                            if (null != listener) {
                                toast("评论成功");
                                listener.OnAddComment(true);
                            }
                        } else {
                            if (null != listener) {
                                toast("评论失败");
                                listener.OnAddComment(false);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        if (null != listener) {
                            toast("评论失败");
                            listener.OnAddComment(false);
                        }
                    }
                } else {
                    onError(null, "数据异常");
                    if (null != listener) {
                        toast("评论失败");
                        listener.OnAddComment(false);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
//                    toast(errorMsg);
                if (null != listener) {
                    toast("评论失败");
                    listener.OnAddComment(false);
                }
            }

            @Override
            public void onEnd() {
            }
        });


    }

    public interface OnAttentionListener {
        void OnAttention(boolean isSuccess, long attentionedUserNums);

        void OnUnAttention(boolean isSuccess, long attentionedUserNums);
    }

    public interface OnAddCommentListener {
        void OnAddComment(boolean isSuccess);
    }
}
